#include "HorBarChart.h"
#include "ui_HorBarChart.h"

HorBarChart::HorBarChart(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HorBarChart)
{
    ui->setupUi(this);
    initUI();
    HandleHelper::GetInstance()->_send2Chart.connect(boost::bind(&HorBarChart::flushData, this, _1));
}

HorBarChart::~HorBarChart()
{
    delete ui;
}

void HorBarChart::initUI()
{
    chart = new QChart;
    chart->legend()->show();

    QStringList list;
    list<<"x"<<"y"<<"z"<<"a"<<"b"<<"c";



    QHorizontalBarSeries *series = new QHorizontalBarSeries;

    curSerial.reset(new QBarSet("value"));
    *curSerial.data()<<0<<0<<0<<0<<0<<0;
    series->append(curSerial.data());
    chart->addSeries(series);

    QBarCategoryAxis *axisY = new QBarCategoryAxis();
    axisY->append(list);
    chart->setAxisY(axisY, series);
    QValueAxis *axisX = new QValueAxis();
    chart->setAxisX(axisX, series);
    axisX->applyNiceNumbers();
    axisX->setMax(1024);
    chart->legend()->setAlignment(Qt::AlignBottom);
    chart->setAnimationOptions(QChart::SeriesAnimations);

    ui->widget->setChart(chart);
}

void HorBarChart::flushData(double *it)
{
    for(int i =0;i<6;i++){
        curSerial.data()->replace(i,it[i]);
    }
}

void HorBarChart::on_theme_currentIndexChanged(int index)
{
    QChart::ChartTheme theme = static_cast<QChart::ChartTheme>(index);

    if (chart->theme() != theme) {
        chart->setTheme(theme);
        QPalette pal = window()->palette();
        if (theme == QChart::ChartThemeLight) {
            pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
            pal.setColor(QPalette::WindowText, QRgb(0x404044));
        } else if (theme == QChart::ChartThemeDark) {
            pal.setColor(QPalette::Window, QRgb(0x121218));
            pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
        } else if (theme == QChart::ChartThemeBlueCerulean) {
            pal.setColor(QPalette::Window, QRgb(0x40434a));
            pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
        } else if (theme == QChart::ChartThemeBrownSand) {
            pal.setColor(QPalette::Window, QRgb(0x9e8965));
            pal.setColor(QPalette::WindowText, QRgb(0x404044));
        } else if (theme == QChart::ChartThemeBlueNcs) {
            pal.setColor(QPalette::Window, QRgb(0x018bba));
            pal.setColor(QPalette::WindowText, QRgb(0x404044));
        } else if (theme == QChart::ChartThemeHighContrast) {
            pal.setColor(QPalette::Window, QRgb(0xffab03));
            pal.setColor(QPalette::WindowText, QRgb(0x181818));
        } else if (theme == QChart::ChartThemeBlueIcy) {
            pal.setColor(QPalette::Window, QRgb(0xcee7f0));
            pal.setColor(QPalette::WindowText, QRgb(0x404044));
        } else {
            pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
            pal.setColor(QPalette::WindowText, QRgb(0x404044));
        }
        window()->setPalette(pal);
    }
}


void HorBarChart::on_next_clicked()
{
    emit updateChart(0);
}
